<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="visible"
    title="关联秒杀商品"
    width="75%"
  >
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="selections.length === 0"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table
      :data="dataList"
      border
      v-loading="loading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="skuName" header-align="center" align="center" label="商品名称">
        <template slot-scope="scope">
          <el-tooltip v-if="scope.row.skuName" class="item" effect="dark" :content="scope.row.skuName"
                      placement="top-start">
            <span> {{ scope.row.skuName.length <= 20 ? scope.row.skuName : (scope.row.skuName.substr(0, 20) + '...') }} </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="price" header-align="center" align="center" label="秒杀价格"></el-table-column>
      <el-table-column prop="count" header-align="center" align="center" label="秒杀总量"></el-table-column>
      <el-table-column prop="limited" header-align="center" align="center" label="每人限购数量"></el-table-column>
      <el-table-column prop="sort" header-align="center" align="center" label="排序"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="small" @click="handleUpdate(scope.row.id)">修改</el-button>
          <el-button type="text" icon="el-icon-delete" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="loadData"
    />
    <seckill-sku-relation-form :sessionId="seckillId" ref="dataForm" @ok="loadData"></seckill-sku-relation-form>

    <span slot="footer" class="dialog-footer">
      <el-button size="mini" icon="el-icon-circle-close" @click="visible = false">关闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import seckillSkuRelationForm from "./seckillSkuRelationForm.vue";
import {deleteSeckillSkuRelation, seckillSkuRelationList} from "@/api/coupon/seckillSkuRelation";

export default {
  data() {
    return {
      seckillId: '',
      visible: false,
      dataList: [],
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      total: 0,
      loading: false,
      selections: [],
      addOrUpdateVisible: false
    };
  },
  props: {
    seckillId: {
      type: String,
      default: ''
    }
  },
  components: {
    seckillSkuRelationForm
  },

  methods: {
    // 获取数据列表
    loadData(seckillId) {
      this.loading = true;
      this.visible = true
      seckillSkuRelationList({seckillId}).then(res => {
        if (res.success) {
          this.dataList = res.data.records;
          this.total = res.data.total;
        }
        this.loading = false;
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.selections = val;
    },
    handleAdd() {
      this.$refs.dataForm.add()
    },
    handleUpdate(id) {
      this.$refs.dataForm.edit(id)
    },

    // 删除
    handleDelete(id) {
      let ids = id ? [id] : this.selections.map(item => {return item.id;});
      this.$confirm(
        '确定删除选中数据吗?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        deleteSeckillSkuRelation(ids).then(res => {
          if (res.success) {
            this.loadData();
            this.$message.success('删除成功')
          }
        });
      });
    }
  }
};
</script>
